package com.kkb.wx.contorller;

import com.kkb.controller.BaseController;
import com.kkb.domain.AjaxResult;
import com.kkb.pojo.TCourse;
import com.kkb.pojo.TDict;
import com.kkb.wx.service.CommonService;
import com.kkb.wx.utils.LocationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//公共接口控制类
@Controller
public class CommonController extends BaseController {
    @Autowired
    private CommonService commonService;

    //获取城市
    @PostMapping("common/citys")
    @ResponseBody
    public AjaxResult getCitys(@RequestBody HashMap data){
        try{
            logger.debug("data {} " +data);
            //通过地址信息，解析出城市名称
            String cityName = LocationUtil.parseLocation((String) data.get("location"));
            //调用Service，根据城市名称，获取城市id
            TDict dict = commonService.findDictByCityName(cityName);
            //调用Servic，根据fs的值，获取城市列表
            List<TDict> cityList = commonService.findDictListByFs((Integer) data.get("fs"));

            //返回数据 location：{} citys:[]

            //模拟实现
            Map result = new HashMap();
            TDict city = new TDict();
            city.setId(9);
            city.setTitle("北京");

            result.put("location",dict);
            result.put("citys",cityList);
            return AjaxResult.success(result);
        }catch(Exception e){
            logger.error(e.getMessage());
            return AjaxResult.error("请求失败："+e.getMessage());
        }
    }


    //获取学科列表
    @GetMapping("common/courseList")
    @ResponseBody
    public AjaxResult getCourseList(){
        try {
            List<TCourse> courseList = commonService.getCourseList();
            return AjaxResult.success(courseList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error("请求失败：" + e.getMessage());
        }
    }
}
